﻿// -----------------------------------------------------------------------
// <copyright file="MatLabTasks.cs" company="Hewlett-Packard">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ImageProcessing.ImageProcessor
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using MathWorks.MATLAB.NET.Arrays;
    using MathWorks.MATLAB.NET.Utility;
    using SomeFunctions;

    /// <summary>
    /// MatLabTasks class 
    /// </summary>
    public class MatLabTasks
    {
        /// <summary>
        /// matlab inst
        /// </summary>
        private Functions matlab = new Functions();

        /// <summary>
        /// the image
        /// </summary>
        private double[,] image;

        /// <summary>
        /// Locals the statictics.
        /// </summary>
        /// <param name="orgImage">The org image.</param>
        /// <param name="k0">The k0.</param>
        /// <param name="k1">The k1.</param>
        /// <param name="k2">The k2.</param>
        /// <param name="e">The e.</param>
        /// <param name="winSize">Size of the win.</param>
        /// <returns>local stat image</returns>
        public Entities.IMG_Image LocalStatictics(Entities.IMG_Image orgImage, double k0, double k1, double k2, double e, int winSize)
        {
            try
            {
                this.ConvertImageToDouble(orgImage);
                MWArray temp_arr;
                temp_arr = this.matlab.LEnhStatistics((MWNumericArray)this.image, (MWNumericArray)winSize, (MWNumericArray)e, (MWNumericArray)k0, (MWNumericArray)k1, (MWNumericArray)k2);

                double[,] allCmp = (double[,])temp_arr.ToArray();

                Entities.IMG_Image retImg = new Entities.IMG_Image(orgImage.Width, orgImage.Height);

                for (int i = 0; i < orgImage.Width; i++)
                {
                    for (int j = 0; j < orgImage.Height; j++)
                    {
                        retImg[j, i] = new Entities.Pixel(allCmp[j, i], allCmp[j, i], allCmp[j, i]);
                    }
                }

                return retImg;
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                throw;
            }
        }

        /// <summary>
        /// Locals the equalization.
        /// </summary>
        /// <param name="orgImage">The org image.</param>
        /// <param name="maskSize">Size of the mask.</param>
        /// <returns>equalized image</returns>
        public Entities.IMG_Image LocalEqualization(Entities.IMG_Image orgImage, int maskSize)
        {
            try
            {
                this.ConvertImageToDouble(orgImage);
                MWArray temp_arr;
                temp_arr = this.matlab.LocalEq((MWNumericArray)this.image, (MWNumericArray)maskSize);

                double[,] allCmp = (double[,])temp_arr.ToArray();

                Entities.IMG_Image retImg = new Entities.IMG_Image(orgImage.Width, orgImage.Height);

                for (int i = 0; i < orgImage.Width; i++)
                {
                    for (int j = 0; j < orgImage.Height; j++)
                    {
                        retImg[j, i] = new Entities.Pixel(allCmp[j, i], allCmp[j, i], allCmp[j, i]);
                    }
                }

                return retImg;
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                throw;
            }
        }

        /// <summary>
        /// Converts the image to double.
        /// </summary>
        /// <param name="orgImage">The org image.</param>
        private void ConvertImageToDouble(Entities.IMG_Image orgImage)
        {
            try
            {
                this.image = new double[orgImage.Height, orgImage.Width];
                for (int i = 0; i < orgImage.Width; i++)
                {
                    for (int j = 0; j < orgImage.Height; j++)
                    {
                        this.image[j, i] = orgImage[j, i].Red + orgImage[j, i].Green + orgImage[j, i].Blue;
                        this.image[j, i] /= 3;
                    }
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                throw;
            }
        }
    }
}
